/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import beans.producto;
import dao.DAOProducto;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author pcsijbug
 */
public class BuscarproductoServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet BuscarArancelServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet BuscarArancelServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        
        String tipo = request.getParameter("hValor");
        
        System.out.println(tipo + "doGet--------------------------------------");
        
        if (tipo.equals("m")) {
            direcionaModificar(request, response);
        }


    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String tipo = request.getParameter("hValor");
        
        System.out.println(tipo + "doPost -----------------------------------------");
        
        if (tipo.equals("b"))
            buscar(request, response);
        else if (tipo.equals("mg")) 
            modificar(request, response);
        else if (tipo.equals("gr")) 
            agregar(request, response);
        


    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    void buscar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //recepcion de parametros
        String criterio = request.getParameter("txtBuscar");

        //llamamos al DAO
        dao.DAOProducto dao = new DAOProducto();

        List listaProducto;
        try {
            listaProducto = dao.buscarProducto(criterio);
            request.setAttribute("listaProducto", listaProducto);
            request.setAttribute("txtBuscar", criterio);
            request.setAttribute("totalProdcutoConsulta", listaProducto.size());
        } catch (SQLException ex) {
            Logger.getLogger(BuscarproductoServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
        request.getRequestDispatcher("/busqueda.jsp").forward(request, response);
    }

    void direcionaModificar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int codigo = Integer.parseInt(request.getParameter("vcod"));

        dao.DAOProducto dao = new DAOProducto();
        producto bean = null;
        try {
            bean = dao.buscarxCodigo(codigo);
        } catch (SQLException ex) {
            Logger.getLogger(BuscarproductoServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        request.setAttribute("Producto", bean);

        request.getRequestDispatcher("/AgregarProyecto.jsp").forward(request, response);

    }

    void modificar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
      

        dao.DAOProducto dao = new DAOProducto();
        producto prd = new producto();
        boolean res = true;
        
        
        int PRODUCT_ID = Integer.parseInt(request.getParameter("txtPRODUCT_ID"));
        
        int MANUFACTURER_ID = Integer.parseInt(request.getParameter("txtMANUFACTURER_ID")); 
        
        String PRODUCT_CODE = request.getParameter("txtPRODUCT_CODE");
        
        double PURCHASE_COST = Double.parseDouble(request.getParameter("txtPURCHASE_COST"));
        
        int QUANTITY_ON_HAND = Integer.parseInt(request.getParameter("txtQUANTITY_ON_HAND"));
        
        double MARKUP = Double.parseDouble(request.getParameter("txtMARKUP"));
        
        String AVAILABLE = request.getParameter("txtAVAILABLE");
        
        String DESCRIPTION = request.getParameter("txtDESCRIPTION");
        
        
        prd.setPRODUCT_ID(PRODUCT_ID);
        prd.setMANUFACTURER_ID(MANUFACTURER_ID);
        prd.setPRODUCT_CODE(PRODUCT_CODE);
        prd.setPURCHASE_COST(PURCHASE_COST);
        prd.setQUANTITY_ON_HAND(QUANTITY_ON_HAND);
        prd.setMARKUP(MARKUP);
        prd.setAVAILABLE(AVAILABLE);
        
        System.out.println(MANUFACTURER_ID + " MANUFACTURER_ID---------------------------->"); 
        
        prd.setDESCRIPTION(DESCRIPTION);
        
        String str = "N";
        
        try {
            res = dao.ModificarProducto(prd);
            
        } catch (SQLException ex) {
            //Logger.getLogger(BuscarproductoServlet.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex.getMessage() + "---------------------------------------> error sql");
        }
        
        if(res == true)
            str = "S";
        
        System.out.println(res);
        
        request.setAttribute("cerrar", str);
        request.setAttribute("Producto", prd);
        
        request.getRequestDispatcher("/AgregarProyecto.jsp").forward(request, response);

    }

    void agregar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
      
        System.out.println("---------------------------------------> agregar1");
        
        dao.DAOProducto dao = new DAOProducto();
        producto prd = new producto();
        boolean res = true;
        
        
        int PRODUCT_ID = Integer.parseInt(request.getParameter("txtPRODUCT_ID"));
        
        int MANUFACTURER_ID = Integer.parseInt(request.getParameter("txtMANUFACTURER_ID"));
        
        String PRODUCT_CODE = request.getParameter("txtPRODUCT_CODE");
        
        double PURCHASE_COST = Double.parseDouble(request.getParameter("txtPURCHASE_COST"));
        
        
        int QUANTITY_ON_HAND = Integer.parseInt(request.getParameter("txtQUANTITY_ON_HAND"));
        
        double MARKUP = Double.parseDouble(request.getParameter("txtMARKUP"));
        
        String AVAILABLE = request.getParameter("txtAVAILABLE");
        
        String DESCRIPTION = request.getParameter("txtDESCRIPTION");
       
        
        prd.setPRODUCT_ID(PRODUCT_ID);
        prd.setMANUFACTURER_ID(MANUFACTURER_ID);
        prd.setPRODUCT_CODE(PRODUCT_CODE);
        prd.setPURCHASE_COST(PURCHASE_COST);
        prd.setQUANTITY_ON_HAND(QUANTITY_ON_HAND);
        prd.setMARKUP(MARKUP);
        prd.setAVAILABLE(AVAILABLE);
        prd.setDESCRIPTION(DESCRIPTION);
        
        String str = "N";
        
        try {
            res = dao.agregarProducto(prd);
            
        } catch (SQLException ex) {
            //Logger.getLogger(BuscarproductoServlet.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex.getMessage() + "---------------------------------------> error sql");
        }
        
        if(res == true)
            str = "S";
        
        System.out.println(res);
        
        request.setAttribute("cerrar", str);
        
        request.getRequestDispatcher("/AgregarProyecto.jsp").forward(request, response);

    }

}



